#include "../include/netdisk.h"
extern extra_data_t globaltoken;
int cd_Cmd(int sockfd, train_t *train)
{
    extra_data_t *newToken = (extra_data_t *)train->data;
    memcpy(&globaltoken, newToken, sizeof(extra_data_t));
    // printf("test: id:|%d|, usrname:|%s|, usrtoken:|%s|\n", globaltoken.cur_dir_id, globaltoken.user, globaltoken.token);
    char *tempdata = train->data + sizeof(extra_data_t);
    // printf("tempdata = |%s|\n", tempdata);
    // 调用ls,展示当前目录
    memset(train, 0, sizeof(train_t));
    train->cmdstat = LS;
    memcpy(train->data, &globaltoken, sizeof(globaltoken));
    memcpy(train->data + sizeof(globaltoken), ".", 1);
    train->length = sizeof(globaltoken) + 1;
    send_train(sockfd, train);
    return 0;
}
int ls_Cmd(train_t *train)
{
    extra_data_t *newToken = (extra_data_t *)train->data;
    memcpy(&globaltoken, newToken, sizeof(extra_data_t));
    // printf("test: id:|%d|, usrname:|%s|, usrtoken:|%s|\n", globaltoken.cur_dir_id, globaltoken.user, globaltoken.token);
    char *tempdata = train->data + sizeof(extra_data_t);

    dir_item_t *dtt = (dir_item_t *)(tempdata + sizeof(int));

    if(dtt->id > 0)
    printf("文件名: |%s|, 文件类型: |%s| \n", dtt->file_name, dtt->type);
    else
    printf("空\n");
    // if (dirs->size > 0)
    // {
    //     printf("文件名: |%s|, 文件类型: |%s| \n", dtt->file_name, dtt->type);
    // }
    // else
    // {
    //     printf("空\n");
    // }
    // dtt = (dir_item_t *)((tempdata + sizeof(int)) + sizeof(dir_item_t));
    // printf("|%s| |%s| \n", dtt->file_name, dtt->type);
    dir_list_t *dirs = (dir_list_t *)tempdata;
    dirs->item_head = (dir_item_t *)(tempdata + sizeof(int));
    for (int i = 1; i < dirs->size; i++)
    {
        /* code */
        // dir_item_t dir_item;
        // printf("|%s| |%s| \n", dirs->item_head[i].file_name, dirs->item_head[i].type);
        dir_item_t *dtt = (dir_item_t *)(tempdata + sizeof(int) + i * sizeof(dir_item_t));
        // dir_item_t *dtt = (dir_item_t *)((tempdata + sizeof(int)));
        printf("文件名: |%s|, 文件类型: |%s| \n", dtt->file_name, dtt->type);
    }

    return 0;
}
int pwd_Cmd(int sockfd, train_t *train)
{
    extra_data_t *newToken = (extra_data_t *)train->data;
    memcpy(&globaltoken, newToken, sizeof(extra_data_t));
    // printf("test: id:|%d|, usrname:|%s|, usrtoken:|%s|\n", globaltoken.cur_dir_id, globaltoken.user, globaltoken.token);
    char *tempdata = train->data + sizeof(extra_data_t);
    // printf("tempdata = |%s|\n", tempdata);
    // 调用ls,展示当前目录
    memset(train, 0, sizeof(train_t));
    train->cmdstat = LS;
    memcpy(train->data, &globaltoken, sizeof(globaltoken));
    memcpy(train->data + sizeof(globaltoken), ".", 1);
    train->length = sizeof(globaltoken) + 1;
    send_train(sockfd, train);
    return 0;
}
int mkdir_Cmd(int sockfd, train_t *train)
{
    extra_data_t *newToken = (extra_data_t *)train->data;
    memcpy(&globaltoken, newToken, sizeof(extra_data_t));
    // printf("test: id:|%d|, usrname:|%s|, usrtoken:|%s|\n", globaltoken.cur_dir_id, globaltoken.user, globaltoken.token);
    char *tempdata = train->data + sizeof(extra_data_t);
    // printf("tempdata = |%s|\n", tempdata);
    // 调用ls,展示当前目录
    memset(train, 0, sizeof(train_t));
    train->cmdstat = LS;
    memcpy(train->data, &globaltoken, sizeof(globaltoken));
    memcpy(train->data + sizeof(globaltoken), ".", 1);
    train->length = sizeof(globaltoken) + 1;
    send_train(sockfd, train);
    return 0;
}
int rmdir_Cmd(int sockfd, train_t *train)
{
    extra_data_t *newToken = (extra_data_t *)train->data;
    memcpy(&globaltoken, newToken, sizeof(extra_data_t));
    // printf("test: id:|%d|, usrname:|%s|, usrtoken:|%s|\n", globaltoken.cur_dir_id, globaltoken.user, globaltoken.token);
    char *tempdata = train->data + sizeof(extra_data_t);
    // printf("tempdata = |%s|\n", tempdata);
    // 调用ls,展示当前目录
    memset(train, 0, sizeof(train_t));
    train->cmdstat = LS;
    memcpy(train->data, &globaltoken, sizeof(globaltoken));
    memcpy(train->data + sizeof(globaltoken), ".", 1);
    train->length = sizeof(globaltoken) + 1;
    send_train(sockfd, train);
    return 0;
}
int remove_Cmd(int sockfd, train_t *train)
{
    extra_data_t *newToken = (extra_data_t *)train->data;
    memcpy(&globaltoken, newToken, sizeof(extra_data_t));
    // printf("test: id:|%d|, usrname:|%s|, usrtoken:|%s|\n", globaltoken.cur_dir_id, globaltoken.user, globaltoken.token);
    char *tempdata = train->data + sizeof(extra_data_t);
    // printf("tempdata = |%s|\n", tempdata);
    // 调用ls,展示当前目录
    memset(train, 0, sizeof(train_t));
    train->cmdstat = LS;
    memcpy(train->data, &globaltoken, sizeof(globaltoken));
    memcpy(train->data + sizeof(globaltoken), ".", 1);
    train->length = sizeof(globaltoken) + 1;
    send_train(sockfd, train);
    return 0;
}
